//
//  FindDetailCollectionViewCell.m
//  TasteEnjoy
//
//  Created by XuRui on 16/5/1.
//  Copyright © 2016年 XuRui. All rights reserved.
//

#import "FindDetailCollectionViewCell.h"

@interface FindDetailCollectionViewCell ()
//14
@property (nonatomic, retain) BaseImageView *imgPhoto; /**<图片*/


@property (nonatomic, retain) BaseLabel *labelMoney; /**< 钱 */

@property (nonatomic, retain) BaseImageView *imgPinglun; /**<评论图标*/
@property (nonatomic, retain) BaseLabel *labelPingCount; /**<评论数*/


@property (nonatomic, retain) BaseImageView *imgLike; /**<点赞图标*/
@property (nonatomic, retain) BaseLabel *labelLikeCount; /**<点赞数*/


@property (nonatomic, retain) BaseImageView *imgCollect; /**<收藏图标*/
@property (nonatomic, retain) BaseLabel *labelCollectCount; /**<收藏人数*/

@property (nonatomic, retain) BaseImageView *imgShopCar; /**<购物车图标*/

@property (nonatomic, retain) BaseView *line; /**<用户上方一条线*/

@property (nonatomic, retain) BaseImageView *imgUserPhoto; /**<用户头像*/
@property (nonatomic, retain) BaseLabel *labelUserTitle; /**<用户上方文字*/

@end
@implementation FindDetailCollectionViewCell


#pragma mark - ------------ 内存 管理 ---------------
- (void)dealloc
{
    [_imgPhoto release];
    [_labelContent release];
    [_labelMoney release];
    [_imgPinglun release];
    [_labelPingCount release];
    
    [_imgLike release];
    [_labelLikeCount release];
    
    [_imgCollect release];
    [_labelCollectCount release];
    
    [_imgShopCar release];
    [_line release];
    
    [_imgUserPhoto release];
    [_labelUserTitle release];
    [_labelUserName release];
    [_model release];
    
    [super dealloc];
}

#pragma mark - ------------ 初始化 ---------------
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self cretrSub];
/** 夜间 */
   self.contentView.dk_backgroundColorPicker = DKColorWithColors([UIColor whiteColor],[UIColor lightGrayColor]);
    }
    return self;
}

#pragma mark - ------------ 创建子视图 ---------------
- (void)cretrSub
{

    self.imgPhoto = [[BaseImageView alloc]init];
    [self.contentView addSubview:_imgPhoto];

    //文本
    self.labelContent = [[BaseLabel alloc]init];
    [self.contentView addSubview:_labelContent];
    _labelContent.numberOfLines = 6;
    _labelContent.textColor = [UIColor grayColor];
    _labelContent.font = [UIFont systemFontOfSize:14];

    //钱
    self.labelMoney = [[BaseLabel alloc]init];
    [self.contentView addSubview:_labelMoney];
    
    
    //评论
    self.imgPinglun = [[BaseImageView alloc]init];
    [self.contentView addSubview:_imgPinglun];
    
    self.labelPingCount = [[BaseLabel alloc]init];
    [self.contentView addSubview:_labelPingCount];
    _labelPingCount.font = [UIFont systemFontOfSize:11];
/** 夜间 */
    _labelPingCount.dk_textColorPicker = DKColorWithColors([UIColor lightGrayColor],[UIColor grayColor]);
    
    //赞
    self.imgLike = [[BaseImageView alloc]init];
    [self.contentView addSubview:_imgLike];
    
    self.labelLikeCount = [[BaseLabel alloc]init];
    [self.contentView addSubview:_labelLikeCount];
    _labelLikeCount.font = [UIFont systemFontOfSize:11];
/** 夜间 */
    _labelLikeCount.dk_textColorPicker = DKColorWithColors([UIColor lightGrayColor],[UIColor grayColor]);
    
    //收藏
    self.imgCollect = [[BaseImageView alloc]init];
    [self.contentView addSubview:_imgCollect];
    
    self.labelCollectCount = [[BaseLabel alloc]init];
    [self.contentView addSubview:_labelCollectCount];
     _labelCollectCount.font = [UIFont systemFontOfSize:11];
/** 夜间 */
    _labelCollectCount.dk_textColorPicker = DKColorWithColors([UIColor lightGrayColor],[UIColor grayColor]);
    
    //购物车
    self.imgShopCar = [[BaseImageView alloc]init];
    [self.contentView addSubview:_imgShopCar];

    
    //线
    self.line = [[BaseView alloc]init];
    [self.contentView addSubview:_line];
    
    //用户
    self.imgUserPhoto = [[BaseImageView alloc]init];
    [self.contentView addSubview:_imgUserPhoto];
    _imgUserPhoto.layer.cornerRadius = 20;
    _imgUserPhoto.contentMode = UIViewContentModeScaleAspectFill;
    _imgUserPhoto.clipsToBounds = YES;
    
    //用户title
    self.labelUserTitle = [[BaseLabel alloc]init];
    [self.contentView addSubview:_labelUserTitle];
    _labelUserTitle.font = [UIFont systemFontOfSize:15];
    _labelUserTitle.textColor = [UIColor grayColor];
    
    //by 用户名
    self.labelUserName = [[BaseLabel alloc]init];
    [self.contentView addSubview:_labelUserName];
    _labelUserName.font = [UIFont systemFontOfSize:14];
/** 夜间 */
    _labelUserName.dk_textColorPicker = DKColorWithColors([UIColor lightGrayColor],[UIColor grayColor]);
    
    [_imgPhoto release];
    [_labelContent release];
    [_labelMoney release];
    [_imgPinglun release];
    [_labelPingCount release];
    
    [_imgLike release];
    [_labelLikeCount release];
    
    [_imgCollect release];
    [_labelCollectCount release];
    
    [_imgShopCar release];
    [_line release];
    
    [_imgUserPhoto release];
    [_labelUserTitle release];
    [_labelUserName release];
    
    
//    _imgPhoto.backgroundColor = [UIColor redColor];
//    _labelContent.backgroundColor = [UIColor redColor];
     _labelMoney.backgroundColor = [UIColor grayColor];
//    _imgPinglun.backgroundColor = [UIColor redColor];
//   _labelPingCount.backgroundColor = [UIColor yellowColor];
//    
//    _imgLike.backgroundColor = [UIColor redColor];
//   _labelLikeCount.backgroundColor = [UIColor yellowColor];
//    
//    _imgCollect.backgroundColor = [UIColor redColor];
//    _labelCollectCount.backgroundColor = [UIColor yellowColor];
    
    _imgShopCar.backgroundColor = [UIColor redColor];
    _line.backgroundColor = [UIColor grayColor];
    
//    _imgUserPhoto.backgroundColor = [UIColor redColor];
//    _labelUserTitle.backgroundColor = [UIColor yellowColor];
//    _labelUserName.backgroundColor = [UIColor redColor];

}



#pragma mark - ------------ 赋值 并在这里 赋大小 ---------------
- (void)setModel:(FindDeatilModel *)model
{

    if (_model != model) {
        [_model release];
        _model = [model retain];
    
    
        [self trans];
        
        [self configFrame];
    
    [_imgPhoto sd_setImageWithURL:[NSURL URLWithString:[model.path stringByReplacingOccurrencesOfString:@"_webp" withString:@""]] placeholderImage:PLACEHOLDER];
    
    _imgPinglun.image = [UIImage imageNamed:@"pinglun"];
    _labelPingCount.text = model.reply_count.stringValue;
    
    _imgLike.image = [UIImage imageNamed:@"zanFind"];
    _labelLikeCount.text = model.like_count.stringValue;
    
    _imgCollect.image = [UIImage imageNamed:@"collect"];
    _labelCollectCount.text = model.favorite_count.stringValue;

    
     [_imgUserPhoto sd_setImageWithURL:[NSURL URLWithString:model.avatar] placeholderImage:PLACEHOLDER];
     _labelUserTitle.text = model.name;

    _labelUserName.text = [@"by " stringByAppendingString:model.username];

    }
}

#pragma mark - ------------ 赋大小 ---------------
- (void)configFrame
{
   //图片 比例
 
 
    CGFloat imgH = self.contentView.frame.size.width * _model.height.intValue / _model.width.intValue;
    
    [_imgPhoto mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.left.right.mas_equalTo(self.contentView).offset(0);
        make.height.mas_equalTo(imgH);
    }];
    
    //文字 自适应
    CGFloat contentH = [[self class]heightWithText:_model.msg];
   
    if (contentH > 15.51 * 6) {
        contentH = 15.51 * 6;
    }
   
    [_labelContent mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(_imgPhoto.mas_bottom).offset(10);
        make.left.mas_equalTo(self.contentView).mas_equalTo(10);
        make.right.mas_equalTo(self.contentView).mas_equalTo(-10);
        make.height.mas_equalTo(contentH);
        
    }];
    
    //下一排 评论
    [_imgPinglun mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.top.mas_equalTo(_labelContent.mas_bottom).offset(10);
        make.left.mas_equalTo(_labelContent);
        make.height.width.mas_equalTo(15);
    }];
    
    //评论数 宽度自适应
    CGFloat pingW = [[self class]widthWithText:_model.reply_count.stringValue];
    [_labelPingCount mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(_imgPinglun);
        make.left.mas_equalTo(_imgPinglun.mas_right).offset(5);
        make.height.mas_equalTo(_imgPinglun);
        make.width.mas_equalTo(pingW);
    }];
    
    //赞
    [_imgLike mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_imgPinglun);
        make.left.mas_equalTo(_labelPingCount.mas_right).offset(5);
        make.width.height.mas_equalTo(_imgPinglun);
    }];

    CGFloat zanW = [[self class]widthWithText:_model.like_count.stringValue];
    [_labelLikeCount mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.top.mas_equalTo(_imgPinglun);
        make.left.mas_equalTo(_imgLike.mas_right).offset(5);
        make.height.mas_equalTo(_imgPinglun);
        make.width.mas_equalTo(zanW);
    }];
    
    //收藏
    [_imgCollect mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_imgPinglun);
        make.left.mas_equalTo(_labelLikeCount.mas_right).offset(5);
        make.width.height.mas_equalTo(_imgPinglun);
    }];
    
    CGFloat collectW = [[self class]widthWithText:_model.favorite_count.stringValue];
    [_labelCollectCount mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(_imgPinglun);
        make.left.mas_equalTo(_imgCollect.mas_right).offset(5);
        make.height.mas_equalTo(_imgPinglun);
        make.width.mas_equalTo(collectW);
    }];
    
    [_line mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.top.mas_equalTo(_imgPinglun.mas_bottom).offset(10);
        make.left.right.mas_equalTo(self.contentView).offset(0);
        make.height.mas_equalTo(1);
        
    }];
    
    [_imgUserPhoto mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.top.mas_equalTo(_line.mas_bottom).offset(10);
        make.left.mas_equalTo(self.contentView).offset(5);
        make.width.height.mas_equalTo(40);
        
    }];

    [_labelUserTitle mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.top.mas_equalTo(_imgUserPhoto);
        make.left.mas_equalTo(_imgUserPhoto.mas_right).offset(10);
        make.right.mas_equalTo(self.contentView).offset(-10);
        make.height.mas_equalTo(20);
        
    }];
    
    [_labelUserName mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.top.mas_equalTo(_labelUserTitle.mas_bottom).offset(5);
        make.left.right.mas_equalTo(_labelUserTitle);
        make.height.mas_equalTo(15);
    }];
    
}

- (void)trans
{
    _imgPhoto.translatesAutoresizingMaskIntoConstraints = NO;
    _labelContent.translatesAutoresizingMaskIntoConstraints = NO;
    _labelMoney.translatesAutoresizingMaskIntoConstraints = NO;
    _imgPinglun.translatesAutoresizingMaskIntoConstraints = NO;
    _labelPingCount.translatesAutoresizingMaskIntoConstraints = NO;
    
    _imgLike.translatesAutoresizingMaskIntoConstraints = NO;
    _labelLikeCount.translatesAutoresizingMaskIntoConstraints = NO;
    
    _imgCollect.translatesAutoresizingMaskIntoConstraints = NO;
    
    _imgShopCar.translatesAutoresizingMaskIntoConstraints = NO;
    _line.translatesAutoresizingMaskIntoConstraints = NO;
    
    _imgUserPhoto.translatesAutoresizingMaskIntoConstraints = NO;
    _labelUserTitle.translatesAutoresizingMaskIntoConstraints = NO;
    _labelUserName.translatesAutoresizingMaskIntoConstraints = NO;


}



#pragma mark - ------------ 文本高度自适应 ---------------
+ (CGFloat)heightWithText:(NSString *)text
{

    //宽度一致 高度 不超过这个最大范围
    CGRect rect = [text boundingRectWithSize:CGSizeMake((SCREEN_WIDTH - 30) / 2 - 20, 1000) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:15]} context:nil];
    
    return rect.size.height;
    
}

#pragma mark - ------------ 文本宽度自适应 ---------------
+ (CGFloat)widthWithText:(NSString *)text
{
  
    
    //高度一致 宽度 不超过这个最大范围
    CGRect rect = [text boundingRectWithSize:CGSizeMake((SCREEN_WIDTH - 30) / 2 - 10, 15) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:15]} context:nil];
    
    return rect.size.width;
}




- (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes
{
    [super applyLayoutAttributes:layoutAttributes];
    //这里写 先走这个方法 会获取不到图片高度 导致masrony崩溃
    
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
